package demo4;
import java.util.Arrays;
import java.util.Comparator;

//      三个常用的接口  用哪个接口 取决于业务 一般推荐比较器
//      1.Comparable
//      2.Comparator
//      3.Cloneable
//
class Student implements Comparable<Student> {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public int compareTo(Student o) {//谁调用compareTo谁就是this
//        if (this.age > o.age){
//            return 1;
//        }else if (this.age < o.age){
//            return -1;
//        }else {
//            return 0;
//        }

//        if-else 也可以写成
        return this.age-o.age;
//        return o.age-this.age;//如果这样写就变成从大到小排序

//        不好的点 如果别人要改比较方法呢
//        这个Comparable接口 有个很大的缺点：对类的侵入性非常强 一旦写好了 不敢轻易改动
//        return (int)(this.score-o.score);
//        return this.name.compareTo(o.name);//String是引用类型 不能直接去比较
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}


//      Comparator接口使用   比较器好处：灵活 对类的侵入性弱
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age- o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score- o2.score);
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}





public class Test {
//    简单的排序
    public static void main1(String[] args) {
        int[] array = {1,32,12,43,5};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);//默认从小到大
        System.out.println(Arrays.toString(array));
    }
//    自定义类型的排序
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("haha",13,99.0);
        students[1] = new Student("heihei",18,97.5);
        students[2] = new Student("maka",5,66.6);
        System.out.println(Arrays.toString(students));
        Arrays.sort(students); //Student类没加implements Comparable<Student>  和改compareTo方法之前
        // 会error  不知道用哪种方式排序方法
        System.out.println(Arrays.toString(students));
    }

    public static void main3(String[] args) {
        Student student1 = new Student("haha",19,99.0);
        Student student2 = new Student("heihei",18,97.5);
        if (student1.compareTo(student2)>0){
            System.out.println("student1大");
        }
        System.out.println(student1.compareTo(student2)>0);//为什么打印true因为
//      Student类toString方法没有添加上compareTo
    }



//      有种更好的接口Comparator   可以不需要在原来是类里修改了
    public static void main4(String[] args) {
        Student student1 = new Student("haha", 19, 99.0);
        Student student2 = new Student("heihei", 18, 97.5);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1, student2));
    }


    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("heihei",13,99.0);
        students[1] = new Student("maka",18,97.5);
        students[2] = new Student("haha",5,66.6);
        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        NameComparator nameComparator = new NameComparator();
//        Arrays.sort(students,scoreComparator);//通过分数进行排序
//        Arrays.sort(students,ageComparator);//通过年龄来排序
        Arrays.sort(students,nameComparator);//通过年龄来排序
        System.out.println(Arrays.toString(students));

    }












}
